import unittest

import requests

from api.invest_api import InvestApi
from api.open_account_api import OpenAccountApi
from api.recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.util import html_parser, init_tender_info, clear_user_info
from config import path_cfg


class TestInvestProcess(unittest.TestCase):
    phone = ''

    @classmethod
    def setUpClass(cls) -> None:
        cls.phone = path_cfg.REG_LOGIN_PHONE
        clear_user_info(cls.phone)
        init_tender_info()

    @classmethod
    def tearDownClass(cls) -> None:
        init_tender_info()

    def setUp(self) -> None:
        self.session = requests.Session()
        self.reg_login_api = RegLoginApi(self.session)
        self.open_api = OpenAccountApi(self.session)
        self.recharge_api = RechargeApi(self.session)
        self.invest_api = InvestApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    def test_invest_process(self):
        # 获取注册验证码图片
        img_verify_resp = self.reg_login_api.get_img_verify_code(1)
        self.assertEqual(200, img_verify_resp.status_code)

        # 获取短信验证码
        body = {
            "phone": self.phone,
            "imgVerifyCode": "8888",
            "type": "reg"
        }
        sms_verify_resp = self.reg_login_api.get_sms_verify_code(body)
        self.assertEqual(200, sms_verify_resp.status_code)
        self.assertIn('成功', sms_verify_resp.json().get('description'))

        # 注册
        body = {
            "phone": self.phone,
            "password": "a123456",
            "verifycode": "8888",
            "phone_code": "666666",
            "dy_server": "on"
        }
        user_register_resp = self.reg_login_api.user_register(body)
        self.assertEqual(200, user_register_resp.status_code)
        self.assertIn('成功', user_register_resp.json().get('description'))

        # 登录
        login_req_body = {
            "keywords": self.phone,
            "password": "a123456"
        }
        user_login_resp = self.reg_login_api.user_login(login_req_body)
        self.assertEqual(200, user_login_resp.status_code)
        self.assertIn('成功', user_login_resp.json().get('description'))

        # 实名
        data_dict = {
            'realname': '王华',
            'card_id': '110101199003065378'
        }
        real_name_resp = self.open_api.real_name(data_dict)
        self.assertEqual(200, real_name_resp.status_code)
        self.assertIn('成功', real_name_resp.json().get('description'))

        # 开户
        open_user_resp = self.open_api.open_account()
        self.assertEqual(200, open_user_resp.status_code)
        self.assertEqual(200, open_user_resp.json().get('status'))

        form_data = open_user_resp.json().get('description').get('form')
        parser_data = html_parser(form_data)

        # 第三方开户
        third_open_resp = self.open_api.third_open_account(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', third_open_resp.text)

        # 充值验证码
        img_verify_resp = self.recharge_api.get_img_verify_code('1')
        self.assertEqual(200, img_verify_resp.status_code)

        # 充值
        form_data = {
            "paymentType": "chinapnrTrust",
            "amount": "10000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        recharge_resp = self.recharge_api.recharge(form_data)
        self.assertEqual(200, recharge_resp.status_code)
        self.assertEqual(200, recharge_resp.json().get('status'))

        # 第三方充值
        form_data = recharge_resp.json().get('description').get('form')
        parser_data = html_parser(form_data)
        third_recharge_resp = self.recharge_api.third_recharge(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', third_recharge_resp.text)

        # 借款标数据清理
        init_tender_info()

        # 投资
        form_data = {
            "id": 642,
            "depositCertificate": -1,
            "amount": 500
        }
        invest_resp = self.invest_api.invest(form_data)
        self.assertEqual(200, recharge_resp.status_code)
        self.assertEqual(200, invest_resp.json().get('status'))

        form_data = invest_resp.json().get('description').get('form')
        parser_data = html_parser(form_data)

        # 第三方投资接口成功
        third_invest_resp = self.invest_api.third_invest(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', third_invest_resp.text)
